home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / Advanced I⁄O v2.3 / Advanced i⁄o / vendian_io.cc < prev    next >
Text File  |  1995-06-19  |  8KB  |  329 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /*
  3.  ************************************************************************
  4.  *
  5.  *            Verify integer and bit I/O
  6.  *
  7.  * $Id: vendian_io.cc,v 2.0 1995/02/07 17:38:11 oleg Exp oleg $
  8.  *
  9.  ************************************************************************
  10.  */
  11.  
  12. #include "endian_io.h"
  13. #include <iostream.h>
  14. #include "std.h"
  15.  
  16. #define NO_PIPES 1
  17.  
  18. /*
  19.  *------------------------------------------------------------------------
  20.  *            Reading and checking functions
  21.  */
  22.  
  23. static void read_and_check_long(EndianIn& i_stream, const long ethalon)
  24. {
  25.   long read = i_stream.read_long("Reading a long integer");
  26.   if( read != ethalon )
  27.     _error("The read long int %d differs from what was expected %d",
  28.        read,ethalon);
  29. }
  30.  
  31. static void read_and_check_short(EndianIn& i_stream, const short ethalon)
  32. {
  33.   short read = i_stream.read_short("Reading a short integer");
  34.   if( read != ethalon )
  35.     _error("The read short int %d differs from what was expected %d",
  36.        read,ethalon);
  37. }
  38.  
  39. static void read_and_check_byte(EndianIn& i_stream, const char ethalon)
  40. {
  41.   char read = i_stream.read_byte("Reading a byte");
  42.   if( read != ethalon )
  43.     _error("The read byte %d differs from what was expected %d",
  44.        read,ethalon);
  45. }
  46.  
  47. /*
  48.  *------------------------------------------------------------------------
  49.  *        Reading and writing ethalon patterns
  50.  */
  51.  
  52. static const unsigned long MyPattern [] =
  53.  { 1, (unsigned)-1, 0, 0xffff0000, 0x0000ffff, 0x5a5a5a5a, 0xa5a5a5a5 };
  54.  
  55. static void write_patterns(EndianOut& o_stream)
  56. {
  57.   {
  58.     unsigned long * p = (unsigned long *)MyPattern; 
  59.     for(; (char *)p < (char *)MyPattern + sizeof(MyPattern); p++)
  60.       o_stream.write_long(*p);
  61.   }
  62.   {
  63.     unsigned short * p = (unsigned short *)MyPattern; 
  64.     for(; (char *)p < (char *)MyPattern + sizeof(MyPattern); p++)
  65.       o_stream.write_short(*p);
  66.   }
  67.   {
  68.     unsigned char * p = (unsigned char *)MyPattern; 
  69.     for(; (char *)p < (char *)MyPattern + sizeof(MyPattern); p++)
  70.       o_stream.write_byte(*p);
  71.   }
  72. }
  73.  
  74. static void read_and_check_patterns(EndianIn& i_stream)
  75. {
  76.   {
  77.     unsigned long * p = (unsigned long *)MyPattern; 
  78.     for(; (char *)p < (char *)MyPattern + sizeof(MyPattern); p++)
  79.       read_and_check_long(i_stream,*p);
  80.   }
  81.   {
  82.     unsigned short * p = (unsigned short *)MyPattern; 
  83.     for(; (char *)p < (char *)MyPattern + sizeof(MyPattern); p++)
  84.       read_and_check_short(i_stream,*p);
  85.   }
  86.   {
  87.     unsigned char * p = (unsigned char *)MyPattern; 
  88.     for(; (char *)p < (char *)MyPattern + sizeof(MyPattern); p++)
  89.       read_and_check_byte(i_stream,*p);
  90.   }
  91. }
  92.  
  93. /*
  94.  *------------------------------------------------------------------------
  95.  *            Verify reading/writing integers in
  96.  *          littleendian mode (most significant byte last)
  97.  */
  98.  
  99. static void test_littleendian()
  100. {
  101.   cout << "\n--> Test reading/writing integers in the littleendian mode\n";
  102.  
  103.   cout << "Opening the output stream to file /tmp/aa\n";
  104.   EndianOut stream("/tmp/aa");
  105.   stream.set_littlendian();
  106.  
  107.   cout << "Writing patterns\n";
  108.   write_patterns(stream);
  109.   stream.close();
  110.  
  111.   cout << "Opening the file as a reading stream through cat\n";
  112.   EndianIn istream;
  113. #if defined(NO_PIPES) && NO_PIPES
  114.   istream.open("/tmp/aa");
  115. #else
  116.   istream.open("cat /tmp/aa |");
  117. #endif
  118.   istream.set_littlendian();
  119.  
  120.   cout << "Reading what we've written back\n";
  121.   read_and_check_patterns(istream);
  122.   istream.close();
  123.  
  124.   cout << "\nDone\n";
  125. }
  126.  
  127. /*
  128.  *------------------------------------------------------------------------
  129.  *            Verify reading/writing integers in
  130.  *          bigendian mode (most significant byte first)
  131.  */
  132.  
  133. static void test_bigendian()
  134. {
  135.   cout << "\n--> Test reading/writing integers in the bigendian mode\n";
  136.  
  137.   {
  138.     cout << "Opening the output stream to file /tmp/aa through cat\n";
  139. #if defined(NO_PIPES) && NO_PIPES
  140.     EndianOut stream("/tmp/aa");
  141. #else
  142.     EndianOut stream("| cat > /tmp/aa");
  143. #endif
  144.     stream.set_bigendian();
  145.  
  146.     cout << "Writing patterns\n";
  147.     write_patterns(stream);
  148.                 // Stream should be closed upon destruction
  149.   }
  150.  
  151.   cout << "Opening the file as a reading stream straight\n";
  152.   EndianIn istream;
  153.   sleep(1);
  154.   istream.open("/tmp/aa");
  155.   istream.set_bigendian();
  156.  
  157.   cout << "Reading what we've written back\n";
  158.   read_and_check_patterns(istream);
  159.   istream.close();
  160.  
  161.   cout << "\nDone\n";
  162. }
  163.  
  164. /*
  165.  *------------------------------------------------------------------------
  166.  *        Test the mixed int/bit stream I/O
  167.  */
  168.  
  169. static void test_int_bit_IO()
  170. {
  171.   cout << "\n--> Test mixed int/bit stream I/O with file attachments\n";
  172.  
  173.   cout << "Opening the output stream to file /tmp/aa through cat\n";
  174. #if defined(NO_PIPES) && NO_PIPES
  175.   EndianOut stream("/tmp/aa");
  176. #else
  177.   EndianOut stream("| cat > /tmp/aa");
  178. #endif
  179.   stream.set_bigendian();
  180.  
  181.   cout << "Writing integer patterns\n";
  182.   write_patterns(stream);
  183.  
  184.   cout << "Attaching the bitstream\n";
  185.   BitOut bitstream;
  186.   bitstream.share_with(stream);
  187.  
  188.   cout << "Writing 8 bits of ones followed by 3*8 zero bits several times\n";
  189.   register int i;
  190.   for(i=0; i<5; i++)
  191.   {
  192.     register int i;
  193.     for(i=0; i<8; i++)
  194.       bitstream.put_bit(1);
  195.     for(i=0; i<3*8; i++)
  196.       bitstream.put_bit(0);
  197.   }
  198.   bitstream.put_bit(1);            // Put two extra bits
  199.   bitstream.put_bit(1);
  200.   bitstream.close();
  201.   {
  202.     cout << "Attaching the second bitstream\n";
  203.     BitOut bitstream;
  204.     bitstream.share_with(stream);
  205.  
  206.     cout << "Writing 8 bits of zeros followed by 3*8 one bits several times\n";
  207.     register int i;
  208.     for(i=0; i<5; i++)
  209.     {
  210.       register int i;
  211.       for(i=0; i<8; i++)
  212.     bitstream.put_bit(0);
  213.       for(i=0; i<3*8; i++)
  214.     bitstream.put_bit(1);
  215.     }
  216.     bitstream.put_bit(0);            // Put two extra bits
  217.     bitstream.put_bit(1);
  218.   }
  219.   stream.close();
  220.  
  221.   cout << "Opening the file as a reading stream straight\n";
  222.   EndianIn istream;
  223.   sleep(1);
  224.   istream.open("/tmp/aa");
  225.   istream.set_bigendian();
  226.  
  227.   cout << "Reading what we've written back\n";
  228.   read_and_check_patterns(istream);
  229.  
  230.   cout << "Attaching the input bitstream\n";
  231.   BitIn ibitstream;
  232.   ibitstream.share_with(istream);
  233.  
  234. #ifndef __MWERKS__
  235.  system("ls -l /tmp/aa; od -x /tmp/aa");
  236. #endif 
  237.   cout << "Reading the bit pattern\n";
  238.   for(i=0; i<5; i++)
  239.   {
  240.     register int i;
  241.     for(i=0; i<8; i++)
  242.       assert( ibitstream.get_bit() == 1 );
  243.     for(i=0; i<3*8; i++)
  244.       assert( ibitstream.get_bit() == 0 );
  245.   }
  246.   assert( ibitstream.get_bit() == 1 );
  247.   assert( ibitstream.get_bit() == 1 );
  248.   ibitstream.close();
  249.   {
  250.     cout << "Attaching the second input bitstream\n";
  251.     BitIn ibitstream;
  252.     ibitstream.share_with(istream);
  253.  
  254.     cout << "Reading the bit pattern\n";
  255.     for(i=0; i<5; i++)
  256.     {
  257.       register int i;
  258.       for(i=0; i<8; i++)
  259.     assert( ibitstream.get_bit() == 0 );
  260.       for(i=0; i<3*8; i++)
  261.     assert( ibitstream.get_bit() == 1 );
  262.     }
  263.     assert( ibitstream.get_bit() == 0 );
  264.     assert( ibitstream.get_bit() == 1 );
  265.   }
  266.   cout << "\nDone\n";
  267. }
  268.  
  269. /*
  270.  *------------------------------------------------------------------------
  271.  *        Test the mixed int/bit stream I/O
  272.  */
  273.  
  274. static void test_varbit_int(void)
  275. {
  276.   cout << "\n--> Test reading/writing short ints using variable number of bits"
  277.        << endl;
  278.   
  279.   const short ethalon [] =
  280.   { 0, 1, 2, -1, -31, -17, 31, 15, -32, 63, 10000, 64+512, 64+511,
  281.     -64-511, -4096, -3, -64-512-4095, 64+512+4096, 0, 1
  282.   };
  283.   
  284.   {
  285.     BitOut outbs;
  286.     outbs.open("/tmp/aa");
  287.     assert( outbs.good() );
  288.     for(register int i=0; i<sizeof(ethalon)/sizeof(ethalon[0]); i++)
  289.       outbs.put_short(ethalon[i]);
  290.     outbs.put_bit(1);        // just for the heck of it
  291.     outbs.put_bit(1);
  292.     outbs.put_bit(1);
  293.   }
  294.     
  295.   {
  296.     BitIn inbs;
  297.     inbs.open("/tmp/aa");
  298.     assert( inbs.good() );
  299.     for(register int i=0; i<sizeof(ethalon)/sizeof(ethalon[0]); i++)
  300.     {
  301.       short val_read = inbs.get_short();
  302.       if( val_read != ethalon[i] )
  303.         _error("%d-th read value %d does not match the ethalon %d",
  304.                i,val_read,ethalon[i]);
  305.     }
  306.     assert( inbs.get_bit() == 1 );
  307.     assert( inbs.get_bit() == 1 );
  308.     assert( inbs.get_bit() == 1 );
  309.     inbs.close();
  310.   }
  311.   
  312.   cout << "\nDone\n";
  313. }
  314.  
  315. /*
  316.  *------------------------------------------------------------------------
  317.  *            Root module
  318.  */
  319.  
  320. main()
  321. {
  322.   cout << "\n\n\t\tVerify integer stream I/O in big/little endian modes"
  323.           "\n\t\t\t\tand bit stream I/O\n\n";
  324.   test_littleendian();
  325.   test_bigendian();
  326.   test_int_bit_IO();
  327.   test_varbit_int();
  328. }
  329.